package com.dzf.controller;

import com.dzf.entity.*;
import com.dzf.service.BookTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author DZF
 * 商品类型 的 公共模块
 */
@RestController
@RequestMapping("/api/public")
public class PublicController {

    @Autowired
    BookTypeService bookTypeService;

    /**
     * 多层类型
     * @return 处理后的 树状 数据
     * {type:
     *  {
     *      1,2,3,4,
     *      5:{1,2,3,4}
     *  },
     *  type2:{
     *      4,5,6,7
     *      5:{1,2,3,4}
     *  }
     * }
     */
    @RequestMapping("/type/findAll2")
    public RespJson findAll2(){
        List<Type> list = bookTypeService.findAll2();
        return RespJson.pack(200,"ok",list);
    }

    /**
     * 商品类型的全查询
     * @return 类型实体
     */
    @RequestMapping("/type/findAll")
    public RespJson findAll()
    {
        List<BookType> bookTypes = bookTypeService.findAll();
        return RespJson.pack(200,"",bookTypes);
    }


    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}
